package logic;

/**
 * class for gathering statistics in game
 * @author Rekin
 *
 */
public class StatisticsManager {
	private static String statsFile = System.getProperty("fileconn.dir.private")+"pkstats.txt";
	private int humanVictories;
	private int gamesPlayed;
	
//	private int humanLoses;
//	private long timePlayed;
//	private long startTime;
//	private int meanTime; 
	
	/**
	 * default constructor
	 */
	public StatisticsManager() {
		humanVictories=0;
		gamesPlayed=0;
		loadDataFromStatsFile();
	}
	
	/**
	 * loads data from statistics file
	 */
	private void loadDataFromStatsFile(){
		String loaded = new FilesManager().loadFromFile(statsFile);
		if(loaded!=null){
			try{
				int xInd = loaded.indexOf("x");
				gamesPlayed = Integer.parseInt(loaded.substring(0, xInd));
				humanVictories = Integer.parseInt(loaded.substring(xInd+1,loaded.length()));
			}
			catch(NumberFormatException e){}
		}
	}
	
	/**
	 * saves statistics to file (when application shuts down)
	 */
	public void saveDataToStatsFile(){
		String toSave = gamesPlayed+"x"+humanVictories;
		new FilesManager().saveToFile(toSave, statsFile);
	}
	
	/**
	 * updates statistics
	 * @param hasHumanWon flag representing if player has won the game
	 */
	public void updateStatistics(boolean hasHumanWon){
		++gamesPlayed;
		if(hasHumanWon){
			++humanVictories;
		}
		else{
			//++humanLoses;
		}
		//some day do meantime: meanTime= now - startTime
	}

	/**
	 * @return humanVictories
	 */
	public int getHumanVictories() {
		return humanVictories;
	}

	/**
	 * @return gamesPlayed
	 */
	public int getGamesPlayed() {
		return gamesPlayed;
	}
}
